//
//  DGPUtil.m
//  大股票
//
//  Created by apple on 2019/8/16.
//  Copyright © 2019 apple. All rights reserved.
//

#import "DGPUtil.h"

@implementation DGPUtil

static  NSArray *_allDatas = nil;

+ (NSArray *)allDatas {
    if (!_allDatas) {
        _allDatas = [NSArray arrayWithContentsOfFile:[NSBundle.mainBundle pathForResource:@"HHYdataList" ofType:@"plist"]];
    }
    return _allDatas;
}


+ (void)addZiXuanModel:(HHYZXStockLocalModel *)model successCallback:(void (^)(void))successCallback failedCallback:(void (^)(void))failedCallback {
    //判断是否包含
    NSString *mid = [NSString stringWithFormat:@"%@%@", [DGPUtil getCurrentUserID], model.symbol
                     ];
    NSString *order = [NSString stringWithFormat:@"where mid = \'%@\'", mid];
    
    NSArray *resArr = [HHYZXStockLocalModel findByCriteria:order];
    
    if (resArr.count > 0) {
        failedCallback();
    } else {
        [model saveOrUpdate];
        successCallback();
    }
    
}

+ (void)requestRandomStockModelWithSuccessCallback:(void (^)(NSArray * _Nonnull))successCallback failedCallback:(void (^)(void))failedCallback {
    int index = arc4random() % ([DGPUtil allDatas].count -10) ;
    NSArray *arr = [[DGPUtil allDatas] subarrayWithRange:NSMakeRange(index, 3)];
    if (arr) {
        successCallback(arr);
    } else {
        failedCallback();
    }
}
//ds
+ (NSString *)getCurrentUserID {
    return  kUserDefaultObjectForKey(@"userid");
}

+(float)money{
    NSArray* moneyarray=kUserDefaultObjectForKey(@"user");
    NSInteger number=[kUserDefaultObjectForKey(@"number") integerValue];
    float money=[moneyarray[number][@"money"] floatValue];
    return money;
}

+(void)calculatemoney:(NSString*)select by:(float)yourmoney successCallback:(void (^)(void))successCallback failedCallback:(void (^)(void))failedCallback{
    
    float nowmoney=[self money];
    
    NSMutableArray* moneyarray=[NSMutableArray arrayWithArray:kUserDefaultObjectForKey(@"user")];
    
    NSInteger number=[kUserDefaultObjectForKey(@"number") integerValue];
    
    NSDictionary* dicc=[moneyarray[number] mutableCopy];
    
    float resultmoney=0.00;
    
    if ([select isEqualToString:@"充值"]) {
        
        resultmoney=nowmoney+yourmoney;
        
        successCallback();
        
    }else if([select isEqualToString:@"提现"]){
        
        if (nowmoney>=yourmoney) {
            
            resultmoney=nowmoney-yourmoney;
            
            successCallback();
            
        }else{
            
            failedCallback();
            return;
        }
    }
    [dicc setValue:[NSNumber numberWithFloat:resultmoney] forKey:@"money"];
    [moneyarray replaceObjectAtIndex:number withObject:dicc];
    kUserDefaultSetObjectForKey(moneyarray, @"user");
}

+ (NSArray *)allApplyModel {
    //判断是否包含

    NSString *order = [NSString stringWithFormat:@"where user = \'%@\'", [self getCurrentUserID]];
    
    NSArray *resArr = [HHYLocalApplyPZModel findByCriteria:order];
    
    return resArr;
}

+(NSString *)getZhiFuPwd{
    NSArray* moneyarray=kUserDefaultObjectForKey(@"user");
    NSInteger number=[kUserDefaultObjectForKey(@"number") integerValue];
    NSString* str=moneyarray[number][@"zhifupwd"];
    if ([str isEqualToString:@"无"]) {
        str=nil;
    }
    return str;
}

@end
